#include "TransparentStaticText.h"
#include <wx/dcclient.h>
#include <wx/tokenzr.h>
#include <wx/graphics.h>

IMPLEMENT_DYNAMIC_CLASS (TransparentStaticText, wxStaticText)

BEGIN_EVENT_TABLE(TransparentStaticText, wxStaticText)
    EVT_PAINT(TransparentStaticText::OnPaint)
END_EVENT_TABLE()
//------------------------------------------------------------
TransparentStaticText::TransparentStaticText() {}
//------------------------------------------------------------
TransparentStaticText::TransparentStaticText(wxWindow* parent, wxWindowID id, const wxString& label,
        const wxPoint& pos, const wxSize& size, long style, const wxString& name )
{
    Create(parent, id, label, pos, size, style, name);
}
//------------------------------------------------------------
bool TransparentStaticText::Create(wxWindow* parent, wxWindowID id, const wxString& label,
                                   const wxPoint& pos, const wxSize& size, long style, const wxString& name )
{
    bool ret = wxStaticText::Create(parent, id, label, pos, size, style|wxTRANSPARENT_WINDOW, name);
    return ret;
}
//------------------------------------------------------------
void TransparentStaticText::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    //wxWindow* parent = GetParent();

    dc.SetFont(GetFont());
    dc.SetBackgroundMode(wxTRANSPARENT);
    dc.SetBackground(*wxTRANSPARENT_BRUSH);
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    dc.SetTextBackground(wxTransparentColour);
    dc.SetTextForeground(GetForegroundColour());

    wxString label = GetLabel();
    wxStringTokenizer tkz(label, _T("\n"), wxTOKEN_RET_EMPTY);
    int ypos = 0;
    while ( tkz.HasMoreTokens() ) {
        wxString token = tkz.GetNextToken();
        wxCoord width;
        wxCoord height;
        wxCoord descent;
        if ( token == _T("") ) {
            dc.GetTextExtent(_T("A"), &width, &height,&descent);
        } else {
            dc.GetTextExtent(token, &width, &height, &descent);
        }
        dc.DrawText(token, 0, ypos);
        ypos += height + descent;
    }
}
